'\" t
.TH "SD_EVENT_WAIT" "3" "" "systemd 257.1" "sd_event_wait"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
sd_event_wait, sd_event_prepare, sd_event_dispatch, sd_event_get_state, sd_event_get_iteration, SD_EVENT_INITIAL, SD_EVENT_PREPARING, SD_EVENT_ARMED, SD_EVENT_PENDING, SD_EVENT_RUNNING, SD_EVENT_EXITING, SD_EVENT_FINISHED \- Low\-level event loop operations
.SH "SYNOPSIS"
.sp
.ft B
.nf
#include <systemd/sd\-event\&.h>
.fi
.ft
.sp
.ft B
.nf
enum {
        \fBSD_EVENT_INITIAL\fR,
        \fBSD_EVENT_PREPARING\fR,
        \fBSD_EVENT_ARMED\fR,
        \fBSD_EVENT_PENDING\fR,
        \fBSD_EVENT_RUNNING\fR,
        \fBSD_EVENT_EXITING\fR,
        \fBSD_EVENT_FINISHED\fR,
};
.fi
.ft
.HP \w'int\ sd_event_prepare('u
.BI "int sd_event_prepare(sd_event\ *" "event" ");"
.HP \w'int\ sd_event_wait('u
.BI "int sd_event_wait(sd_event\ *" "event" ", uint64_t\ " "usec" ");"
.HP \w'int\ sd_event_dispatch('u
.BI "int sd_event_dispatch(sd_event\ *" "event" ");"
.HP \w'int\ sd_event_get_state('u
.BI "int sd_event_get_state(sd_event\ *" "event" ");"
.HP \w'int\ sd_event_get_iteration('u
.BI "int sd_event_get_iteration(sd_event\ *" "event" ", uint64_t\ *" "ret" ");"
.SH "DESCRIPTION"
.PP
The low\-level
\fBsd_event_prepare()\fR,
\fBsd_event_wait()\fR
and
\fBsd_event_dispatch()\fR
functions may be used to execute specific phases of an event loop\&. See
\fBsd_event_run\fR(3)
and
\fBsd_event_loop\fR(3)
for higher\-level functions that execute individual but complete iterations of an event loop or run it continuously\&.
.PP
\fBsd_event_prepare()\fR
checks for pending events and arms necessary timers\&. If any events are ready to be processed ("pending"), it returns a positive, non\-zero value, and the caller should process these events with
\fBsd_event_dispatch()\fR\&.
.PP
\fBsd_event_dispatch()\fR
dispatches the highest priority event source that has a pending event\&. On success,
\fBsd_event_dispatch()\fR
returns either zero, which indicates that no further event sources may be dispatched and exiting of the event loop was requested via
\fBsd_event_exit\fR(3); or a positive non\-zero value, which means that an event source was dispatched and the loop returned to its initial state, and the caller should initiate the next event loop iteration by invoking
\fBsd_event_prepare()\fR
again\&.
.PP
In case
\fBsd_event_prepare()\fR
returned zero,
\fBsd_event_wait()\fR
should be called to wait for further events or a timeout\&. If any events are ready to be processed, it returns a positive, non\-zero value, and the events should be dispatched with
\fBsd_event_dispatch()\fR\&. Otherwise, the event loop returned to its initial state and the next event loop iteration should be initiated by invoking
\fBsd_event_prepare()\fR
again\&.
.PP
\fBsd_event_get_state()\fR
may be used to determine the state the event loop is currently in\&. It returns one of the states described below\&.
.PP
\fBsd_event_get_iteration()\fR
may be used to determine the current iteration of the event loop\&. It returns an unsigned 64\-bit integer containing a counter that increases monotonically with each iteration of the event loop, starting with 0\&. The counter is increased at the time of the
\fBsd_event_prepare()\fR
invocation\&.
.PP
All five functions take, as the first argument, the event loop object
\fIevent\fR
that has been created with
\fBsd_event_new()\fR\&. The timeout for
\fBsd_event_wait()\fR
is specified in
\fIusec\fR
in microseconds\&.
\fB(uint64_t) \-1\fR
may be used to specify an infinite timeout\&.
.SH "STATE MACHINE"
.PP
The event loop knows the following states, that may be queried with
\fBsd_event_get_state()\fR\&.
.PP
\fBSD_EVENT_INITIAL\fR
.RS 4
The initial state the event loop is in, before each event loop iteration\&. Use
\fBsd_event_prepare()\fR
to transition the event loop into the
\fBSD_EVENT_ARMED\fR
or
\fBSD_EVENT_PENDING\fR
states\&.
.sp
Added in version 229\&.
.RE
.PP
\fBSD_EVENT_PREPARING\fR
.RS 4
An event source is currently being prepared, i\&.e\&. the preparation handler is currently being executed, as set with
\fBsd_event_source_set_prepare\fR(3)\&. This state is only seen in the event source preparation handler that is invoked from the
\fBsd_event_prepare()\fR
call and is immediately followed by
\fBSD_EVENT_ARMED\fR
or
\fBSD_EVENT_PENDING\fR\&.
.sp
Added in version 229\&.
.RE
.PP
\fBSD_EVENT_ARMED\fR
.RS 4
\fBsd_event_prepare()\fR
has been called and no event sources were ready to be dispatched\&. Use
\fBsd_event_wait()\fR
to wait for new events, and transition into
\fBSD_EVENT_PENDING\fR
or back into
\fBSD_EVENT_INITIAL\fR\&.
.sp
Added in version 229\&.
.RE
.PP
\fBSD_EVENT_PENDING\fR
.RS 4
\fBsd_event_prepare()\fR
or
\fBsd_event_wait()\fR
have been called and there were event sources with events pending\&. Use
\fBsd_event_dispatch()\fR
to dispatch the highest priority event source and transition back to
\fBSD_EVENT_INITIAL\fR, or
\fBSD_EVENT_FINISHED\fR\&.
.sp
Added in version 229\&.
.RE
.PP
\fBSD_EVENT_RUNNING\fR
.RS 4
A regular event source is currently being dispatched\&. This state is only seen in the event source handler that is invoked from the
\fBsd_event_dispatch()\fR
call, and is immediately followed by
\fBSD_EVENT_INITIAL\fR
or
\fBSD_EVENT_FINISHED\fR
as soon the event source handler returns\&. Note that during dispatching of exit event sources the
\fBSD_EVENT_EXITING\fR
state is seen instead\&.
.sp
Added in version 229\&.
.RE
.PP
\fBSD_EVENT_EXITING\fR
.RS 4
Similar to
\fBSD_EVENT_RUNNING\fR
but is the state in effect while dispatching exit event sources\&. It is followed by
\fBSD_EVENT_INITIAL\fR
or
\fBSD_EVENT_FINISHED\fR
as soon as the event handler returns\&.
.sp
Added in version 229\&.
.RE
.PP
\fBSD_EVENT_FINISHED\fR
.RS 4
The event loop has exited\&. All exit event sources have run\&. If the event loop is in this state it serves no purpose anymore, and should be freed\&.
.sp
Added in version 229\&.
.RE
.PP
A simplified flow chart of the states and the calls to transition between them is shown below\&. Note that
\fBSD_EVENT_PREPARING\fR,
\fBSD_EVENT_RUNNING\fR
and
\fBSD_EVENT_EXITING\fR
are not shown here\&.
.sp
.if n \{\
.RS 4
.\}
.nf
          INITIAL \-<\-\-\-<\-\-\-<\-\-\-<\-\-\-<\-\-\-<\-\-\-<\-\-\-<\-\-\-<\-\-\-<\-\-\-<\-\-\-<\-\-\-\e
             |                                                     |
             |                                                     ^
             |                                                     |
             v                 ret == 0                            |
      sd_event_prepare() >\-\-\->\-\-\->\-\-\->\-\-\->\- ARMED                  |
             |                                |                    ^
             | ret > 0                        |                    |
             |                                |                    |
             v                                v          ret == 0  |
          PENDING <\-\-\-<\-\-\-<\-\-\-<\-\-\-<\-\-\-< sd_event_wait() >\-\-\->\-\-\->\-\-+
             |           ret > 0                                   ^
             |                                                     |
             |                                                     |
             v                                                     |
      sd_event_dispatch() >\-\-\->\-\-\->\-\-\->\-\-\->\-\-\->\-\-\->\-\-\->\-\-\->\-\-\->\-\-\->/
             |                             ret > 0
             | ret == 0
             |
             v
          FINISHED
.fi
.if n \{\
.RE
.\}
.SH "RETURN VALUE"
.PP
On success, these functions return 0 or a positive integer\&. On failure, they return a negative errno\-style error code\&. In case of
\fBsd_event_prepare()\fR
and
\fBsd_event_wait()\fR, a positive, non\-zero return code indicates that events are ready to be processed and zero indicates that no events are ready\&. In case of
\fBsd_event_dispatch()\fR, a positive, non\-zero return code indicates that the event loop returned to its initial state and zero indicates the event loop has exited\&.
\fBsd_event_get_state()\fR
returns a positive or zero state on success\&.
.SS "Errors"
.PP
Returned errors may indicate the following problems:
.PP
\fB\-EINVAL\fR
.RS 4
The
\fIevent\fR
parameter is invalid or
\fBNULL\fR\&.
.RE
.PP
\fB\-EBUSY\fR
.RS 4
The event loop object is not in the right state\&.
.RE
.PP
\fB\-ESTALE\fR
.RS 4
The event loop is already terminated\&.
.RE
.PP
\fB\-ECHILD\fR
.RS 4
The event loop has been created in a different process, library or module instance\&.
.RE
.PP
Other errors are possible, too\&.
.SH "NOTES"
.PP
Functions described here are available as a shared library, which can be compiled against and linked to with the
\fBlibsystemd\fR\ \&\fBpkg-config\fR(1)
file\&.
.PP
The code described here uses
\fBgetenv\fR(3), which is declared to be not multi\-thread\-safe\&. This means that the code calling the functions described here must not call
\fBsetenv\fR(3)
from a parallel thread\&. It is recommended to only do calls to
\fBsetenv()\fR
from an early phase of the program when no other threads have been started\&.
.SH "HISTORY"
.PP
\fBsd_event_prepare()\fR,
\fBsd_event_wait()\fR,
\fBsd_event_dispatch()\fR, and
\fBsd_event_get_state()\fR
were added in version 221\&.
.PP
\fBsd_event_get_iteration()\fR
was added in version 231\&.
.SH "SEE ALSO"
.PP
\fBsystemd\fR(1), \fBsd_event_new\fR(3), \fBsd_event_add_io\fR(3), \fBsd_event_add_time\fR(3), \fBsd_event_add_signal\fR(3), \fBsd_event_add_child\fR(3), \fBsd_event_add_inotify\fR(3), \fBsd_event_add_defer\fR(3), \fBsd_event_run\fR(3), \fBsd_event_get_fd\fR(3), \fBsd_event_source_set_prepare\fR(3)
